﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ZLDC.Common.Estate;

namespace ZLDC.Topo.BLL.Estate
{
    public class ReportBLL
    {
        public static string p_report_dept_list_table(int typeid, string condition)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.ReportDAL.p_report_dept_list_table(typeid, condition, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

        public static string p_report_dept_list_table(int typeid, string condition, int deptid)
        {
            int ismanager = 0; int isStation = 0; string sessionDeptid = _Utility.GetInfoBySession().m_sDeptID; string station = _Utility.GetInfoBySession().m_sStation; if (_Utility.GetWebConfigSetting("SalesManagerStation").Contains(station))
                isStation = 1;

            string deptlist = ZLDC.Common.BaseUser.UserInfo.getAllChildDept(sessionDeptid, "0");

            if (deptlist.Contains(deptid.ToString()) || ZLDC.Common.BaseUser.UserInfo.GetPopedom("查看量化详情"))
                ismanager = 1;

            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.ReportDAL.p_report_dept_list_table(typeid, condition, db);
            }

            _Json json = new _Json(dt);
            return "{page:{ismanager:'" + ismanager + "',station:'" + isStation + "'},record:" + json.ToJson() + "}";
        }


        public static string p_rank_list_table(int typeid, string condition, string orderbyname, int deptid)
        {
            int ismanager = 0; int isStation = 0; string sessionDeptid = _Utility.GetInfoBySession().m_sDeptID; string station = _Utility.GetInfoBySession().m_sStation; if (_Utility.GetWebConfigSetting("SalesManagerStation").Contains(station))
                isStation = 1;

            string deptlist = ZLDC.Common.BaseUser.UserInfo.getAllChildDept(sessionDeptid, "0");

            if (deptlist.Contains(deptid.ToString()) || ZLDC.Common.BaseUser.UserInfo.GetPopedom("查看量化详情"))
                ismanager = 1;

            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.ReportDAL.p_rank_list_table(typeid, condition, orderbyname, db);
            }

            _Json json = new _Json(dt);
            return "{page:{ismanager:'" + ismanager + "',station:'" + isStation + "'},record:" + json.ToJson() + "}";
        }


        public static string p_rank_list_action_table(int typeid, string condition, string orderbyname, int deptid)
        {

            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.ReportDAL.p_rank_list_table(typeid, condition, orderbyname, db);
            }

            _Json json = new _Json(dt);
            return "{deptid:'" + deptid + "',record:" + json.ToJson() + "}";
        }



        public static DataTable p_report_dept_list(int typeid, string condition)
        {
            using (_Database db = new _Database("estate"))
            {
                return ZLDC.DAL.Estate.ReportDAL.p_report_dept_list_table(typeid, condition, db);
            }
        }

        public static DataTable GetReportCountByWeek(string feild, string condition)
        {
            using (_Database db = new _Database("estate"))
            {
                return ZLDC.DAL.Estate.ReportDAL.GetReportCountByWeek(feild, condition, db);
            }
        }

        public static DataTable GetReportCountByMonth(string feild, string condition)
        {
            using (_Database db = new _Database("estate"))
            {
                return ZLDC.DAL.Estate.ReportDAL.GetReportCountByMonth(feild, condition, db);
            }
        }

        public static string GetYeji(string userid, string deptid, string fgs, string beginDate, string endDate)
        {
            StringBuilder strJson = new StringBuilder();

            DataTable dt = null;
            using (_Database db = new _Database("zlhq_report"))
            {
                dt = ZLDC.DAL.Estate.ReportDAL.GetYeji(userid, deptid, fgs, beginDate, endDate, db);
            }

            if (dt.Rows.Count > 0)
            {
                strJson.AppendLine("personsum:'" + dt.Rows[0][0].ToString() + "'");
                strJson.AppendLine(",deptsum:'" + dt.Rows[0][1].ToString() + "'");
                strJson.AppendLine(",personleasecount:'" + dt.Rows[0][2].ToString() + "'");
                strJson.AppendLine(",personleasesum:'" + dt.Rows[0][3].ToString() + "'");
                strJson.AppendLine(",personsellcount:'" + dt.Rows[0][4].ToString() + "'");
                strJson.AppendLine(",personsellsum:'" + dt.Rows[0][5].ToString() + "'");
                strJson.AppendLine(",deptleasecount:'" + dt.Rows[0][6].ToString() + "'");
                strJson.AppendLine(",deptleasesum:'" + dt.Rows[0][7].ToString() + "'");
                strJson.AppendLine(",deptsellcount:'" + dt.Rows[0][8].ToString() + "'");
                strJson.AppendLine(",deptsellsum:'" + dt.Rows[0][9].ToString() + "'");
                strJson.AppendLine(",personranking:'" + dt.Rows[0][10].ToString() + "'");
                strJson.AppendLine(",deptranking:'" + dt.Rows[0][11].ToString() + "'");
            }

            return "{record:{" + strJson.ToString() + "}}"; ;
        }

        public static DataSet GetYejiAnalysis(string beginDate, string endDate, string fgs, int dtype, int deptid)
        {
            DataSet ds = null;
            using (_Database db = new _Database("estate"))
            {
                ds = ZLDC.DAL.Estate.ReportDAL.GetYejiAnalysis(beginDate, endDate, fgs, dtype, deptid, db);
            }
            return ds;
        }

        #region 量化详情

        public static string p_quantificationlog_type_table(int typeid, int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0; int pageall = 0;
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.ReportDAL.p_quantificationlog_type_table(typeid, page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);
            _Json json = new _Json(dt, 1);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

        #endregion
    }
}
